<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
    ></tab-control>
    <scroll
      class="content"
      ref="scroll"
      :probeType="3"
      @scroll="contentScroll"
      :pullUpLoad="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        class="banners"
        ref="swiper"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
//! 此处导入的是大组件
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";

//! 此处导入的是单个页面的小组件
import HomeSwiper from "./childComps/homeSwiper.vue";
import RecommendView from "./childComps/RecommendView.vue";
import FeatureView from "./childComps/Feature.vue";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/backTop/BackTop";

//! 此处导入的是数据
import { getHomeMultidata, getHomeGoods } from "network/home";

//! 这里导入的是防抖动的方法
// import { debounce } from "common/utils";
import { itemListenerMixin, BackTopMixin } from 'common/mixin'

export default {
  name: "Home",
  components: {
    NavBar,
    TabControl,
    GoodsList,
    HomeSwiper,
    RecommendView,
    FeatureView,
    Scroll,
    BackTop,
  },

  mixins: [itemListenerMixin, BackTopMixin],

  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
    };
  },

  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },

  created() {
    //1. 请求多个数据
    this.getHomeMultidata();

    //2. 请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },

  deactivated() {
    // 取消全局事件的监听
    //离开home页面的时候 取消掉这个子组件传过来的加载更多数据的方法
    this.$bus.$off("itemImgLoad", this.itemImgListener);
  },

  mounted() {

  },

  destroyed() {
    console.log("home被销毁了");
  },

  methods: {
    //? 事件监听相关方法
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },

    //? 网络请求相关的方法
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.data.banner.list;
        this.recommends = res.data.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1; //每次请求数据让页码+1
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;

        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      });
    },



    //? 检测滚动超过了500 就显示回到顶部按钮
    contentScroll(position) {
      // if(position.y < -500) {
      //   this.isShowBackTop = true
      // }else {
      //   this.isShowBackTop = false
      // }
      // 1.判断BackTop是否显示
      this.isShowBackTop = -position.y > 500;

      // 2.决定tabControl是否吸顶（position：fixed）
      this.isTabFixed = -position.y - 44 > this.tabOffsetTop;
    },

    //? 上拉加载更多数据loadMore
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    swiperImageLoad() {
      //2. 获取tabControl的offsetTop
      // 所有的组件都有一个属性$el: 用于获取组件中的元素。
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
  },
};
</script>

<style scoped>
/* .banners {
  padding-top: 44px;
} */

.home-nav {
  background-color: var(--color-tint);
  color: white;
}

.content {
  height: calc(100vh - 50px);
  overflow: hidden;
  /* margin-top: 44px; */
  /* padding-top: 44px; */
}

.tab-control {
  display: flex;
  text-align: center;
  font-size: 15px;
  position: relative;
  background-color: #fff;
}
</style>
